<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>采集器管理 - Prometheus 管理器</title>
    <link href="/static/bootstrap.min.css" rel="stylesheet">
    <link href="/static/bootstrap-icons.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="/">Prometheus 管理器</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link active" href="/collectors">采集器</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/alert_rules">告警规则</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/notifications">通知配置</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/alert_history">告警历史</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>采集器管理</h2>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCollectorModal">
                <i class="bi bi-plus-circle"></i> 添加采集器
            </button>
        </div>

        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>名称</th>
                        <th>类型</th>
                        <th>URL</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for collector in collectors %}
                    <tr>
                        <td>{{ collector.id }}</td>
                        <td>{{ collector.name }}</td>
                        <td>{{ collector.type }}</td>
                        <td>{{ collector.url }}</td>
                        <td>
                            {% if collector.enabled %}
                                <span class="badge bg-success">启用</span>
                            {% else %}
                                <span class="badge bg-secondary">禁用</span>
                            {% endif %}
                        </td>
                        <td>{{ collector.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                        <td>
                            <button class="btn btn-sm btn-outline-primary" onclick="editCollector({{ collector.id }})">
                                <i class="bi bi-pencil"></i>
                            </button>
                            <button class="btn btn-sm btn-outline-danger" onclick="deleteCollector({{ collector.id }})">
                                <i class="bi bi-trash"></i>
                            </button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- 添加采集器模态框 -->
    <div class="modal fade" id="addCollectorModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">添加采集器</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="addCollectorForm">
                        <div class="mb-3">
                            <label class="form-label">名称</label>
                            <input type="text" class="form-control" name="name" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">类型</label>
                            <select class="form-select" name="type" required>
                                <option value="">选择类型</option>
                                <option value="node-exporter">node-exporter</option>
                                <option value="cadvisor">cAdvisor</option>
                                <option value="blackbox">Blackbox Exporter</option>
                                <option value="custom">自定义</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">URL</label>
                            <input type="url" class="form-control" name="url" required placeholder="http://host:port/metrics">
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" name="enabled" checked>
                            <label class="form-check-label">启用</label>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="addCollector()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script src="/static/bootstrap.bundle.min.js"></script>
    <script>
        async function addCollector() {
            const form = document.getElementById('addCollectorForm');
            const formData = new FormData(form);
            
            const data = {
                name: formData.get('name'),
                type: formData.get('type'),
                url: formData.get('url'),
                enabled: formData.get('enabled') === 'on'
            };
            
            try {
                const response = await fetch('/api/collectors/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                
                if (response.ok) {
                    location.reload();
                } else {
                    alert('添加采集器失败');
                }
            } catch (error) {
                console.error('Error:', error);
                alert('添加采集器失败');
            }
        }
        
        function editCollector(id) {
            alert('编辑功能待实现 - ID: ' + id);
        }
        
        async function deleteCollector(id) {
            if (confirm('确定要删除这个采集器吗？')) {
                try {
                    const response = await fetch(`/api/collectors/${id}`, {
                        method: 'DELETE'
                    });
                    
                    if (response.ok) {
                        location.reload();
                    } else {
                        alert('删除采集器失败');
                    }
                } catch (error) {
                    console.error('Error:', error);
                    alert('删除采集器失败');
                }
            }
        }
    </script>
</body>
</html>